package com.tedu.controller;

import com.tedu.dao.NewsDao;
import com.tedu.pojo.News;
import com.tedu.pojo.Type;
import com.tedu.servies.NewsServies;
import com.tedu.servies.NewsTypeServies;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * 处理用户的党建信息的请求
 */
@Controller
public class NewsController {

    @Autowired
    NewsDao newsDao;
    @Autowired
    NewsTypeServies newsTypeServies;
    @Autowired
    NewsServies newsServies;


    // 分页获取党建信息
    /**
     *
     * @param type
     * @param Pageno
     * @return
     */
    @RequestMapping("/getNewsByNO")
    public String getNewsByNO(@RequestParam(value = "type",defaultValue = "党建要闻",required = false) String type,
                              @RequestParam(value = "Pageno",defaultValue = "1",required = false) int Pageno
    , Model model){
        System.out.println(type+"=======获取党建信息列表页======"+Pageno);

        //调用dao层获取新闻数据
        List<News> newsByPage = newsDao.getNewsByPage(type, (Pageno - 1) * 20);
        System.out.println(newsByPage);

        //随机获取18个党建类型数据
        List<Type> typeBySize = newsTypeServies.getTypeBySize(12);
        //发送到页面
        model.addAttribute("typeBySize",typeBySize);

        //发送数据到页面
        model.addAttribute("newsByPage",newsByPage);
        model.addAttribute("type",type);
        model.addAttribute("Pageno",Pageno);


        //转发到信息列表页
        return "forward:/WEB-INF/jsp/newslist.jsp";
    }


    /**
     * 阅读一条新闻
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/readNews")
    public String readNews(int id,Model model){

        System.out.println("您正在阅读"+id);
        //调用
       News news =  newsServies.getNewsById(id);
       //发送数据到页面
        model.addAttribute("news",news);
        //调用服务层方法，随机获取12条党建信息类型数据
        List<Type> typeBySize = newsTypeServies.getTypeBySize(10);
        // 发送数据到页面
        model.addAttribute("typeBySize",typeBySize);
        return "forward:/WEB-INF/jsp/readnews.jsp";
    }

}
